


(*Fenetre Principale*)
let _ = 
GMain.init()
 
let namefile= ref "carte.png"
let inter = ref 10
let minr = ref 1
let maxr = ref 10  

  let window = GWindow.window 
  ~height:600
  ~width:800
  ~title:"Touch And Play" ()

let multiplepage=GPack.notebook (*creation de la gestion d'onglets*)
  ~tab_pos: `TOP
  ~packing: window#add ()

(*ajouts des onglets*)
let page1 = GBin.frame
~label:"Preview"   
~packing:multiplepage#add () 

let page2= GBin.frame
  ~label:"Option"
  ~packing:multiplepage#add ()


(*Chargement des conteneur*)
let hbigbox = GPack.box `HORIZONTAL
 ~spacing:10
 ~border_width:10
 ~packing:page1#add () (*nise dans l'onglet 1*)

let hbigbox2 = GPack.box `HORIZONTAL
 ~spacing:100
 ~border_width:120
 ~packing:page2#add () (*nise dans l'onglet 2*)

let vbbox = GPack.hbox
 ~spacing:5
 ~border_width:5
 ~packing:hbigbox#add ()

(*Button Box in Vertical Box(vbbox in Horizontal Box(vbox))*) 
let bbox = GPack.button_box `VERTICAL
 ~layout:`SPREAD
 ~spacing:20
 ~border_width:10
 ~packing:(hbigbox#pack ~expand:false ) ()

let getcont = function
  | Some x -> x
  | _ -> "None"

let convert =function
  |""->0
  |x->int_of_string x
 
(*Drawing area*)
let frame = GBin.frame 
 ~label:"View" 
 ~label_xalign:1.0
 ~shadow_type:`ETCHED_OUT
 ~packing:vbbox#add ()

let vbox_img =GPack.vbox ()

let hbox_img = GPack.hbox
 ~show:true 
 ~packing:vbox_img#add ()

let image_area = GBin.frame
  ~label:"Image"
  ~packing:hbox_img#add ()

let image_view = GMisc.image
  ~file:!namefile
  ~packing:frame#add()

let aff_image filename = image_view#set_file filename

(*Button*)
(*First Tab*)



let may_print btn () = Gaux.may image_view#set_file  btn#filename

let open_file_but =
    let opf = GFile.chooser_button
     ~title:"Browser"
     ~action:`OPEN
     ~width:50
     ~packing:(bbox#pack ~expand:false) () in  
     ignore(opf#connect#selection_changed (may_print opf)); 
     ignore(opf#connect#selection_changed (fun () -> namefile :=(getcont opf#filename)));
     ignore(opf#connect#selection_changed (fun () -> Sys.remove "./scan.bmp"));
     ignore(opf#connect#selection_changed (fun () -> Sys.remove "./bw.bmp"));
     ignore(opf#connect#selection_changed (fun () -> Sys.remove "./cad.bmp"));
    opf

let bw =
let but = GButton.button
    ~label:"Black/White"
    ~packing:bbox#add () in 
let dst = Carto.loading !namefile in 
  ignore(but#connect#clicked (fun()->
			 Carto.image2grey (Carto.loading !namefile)
			   dst));
  ignore(but#connect#clicked (fun() -> Carto.save dst "bw.bmp"));
  ignore(but#connect#clicked  (fun () ->image_view#set_file "bw.bmp"));
ignore(but#connect#clicked (fun () -> namefile := "bw.bmp"));
but


let pre_traitement =
let but1 = GButton.button
  ~label: "Traitement"
  ~packing:bbox#add() in 
let dst1 = Carto.loading !namefile in 
    ignore(but1#connect#clicked (fun () -> Carto.scan (Carto.loading !namefile)
			   dst1 (255,0,0)));
  ignore(but1#connect#clicked (fun () -> Carto.save dst1 "scan.bmp"));
   ignore(but1#connect#clicked (fun () -> image_view#set_file	 "scan.bmp"));
ignore(but1#connect#clicked (fun () -> namefile :=("scan.bmp")));
  but1  
    

let gridbut =
let gridding = GButton.button 
  ~label: "Cadre"
  ~packing:bbox#add() in  
let coordtab = Carto.init_coordtab (Carto.loading !namefile) !inter in
let tab = Carto.create_tab( Carto.loading !namefile) in 
  let dst = Carto.loading !namefile in  
    ignore( gridding#connect#clicked(fun () -> Carto.grid
				       (Carto.loading !namefile)  dst
				       !inter tab coordtab));
   ignore(gridding#connect#clicked (fun () -> Carto.save dst "cad.bmp"));
   ignore(gridding#connect#clicked (fun () -> image_view#set_file
				      "cad.bmp"));
ignore(gridding#connect#clicked (fun () -> namefile :=("cad.bmp")));
   gridding 

let model = GButton.button
    ~label:"Modelize"
    ~packing:bbox#add ()
let saveb = 
let savebut = GButton.button
    ~label:"Save"
    ~packing:bbox#add () in 
let coordtab = Carto.init_coordtab (Carto.loading !namefile) !inter in 
  ignore(savebut#connect#clicked (fun () -> Carto.save (Carto.loading
							  !namefile)
    "Saved.bmp"));
  ignore(savebut#connect#clicked (fun () -> Carto.writecoordstream
				    "TroisD.obj" coordtab !inter));
  savebut  

let quit =
  let button = GButton.button
    ~stock:`QUIT
    ~packing:bbox#add () in
  ignore(button#connect#clicked ~callback:GMain.quit);
       ignore(button#connect#clicked (fun () -> Sys.remove "./scan.bmp"));
     ignore(button#connect#clicked (fun () -> Sys.remove "./bw.bmp"));
     ignore(button#connect#clicked (fun () -> Sys.remove "./cad.bmp"));  
button

(*Second Tab*)
(*Conteneur*)
let scrollWin = GBin.scrolled_window
  ~hpolicy:`ALWAYS
  ~vpolicy:`ALWAYS
  ~packing:hbigbox2#add () 

let lbox = GPack.box `VERTICAL
 ~spacing:2
 ~border_width:10
 ~packing:scrollWin#add () 
      
let gridTxt = GBin.frame 
  ~label:"Cadre"
  ~width: 5
  ~height: 5 
  ~packing:lbox#add() 
 
 let hTxt1 = GBin.frame
  ~label:"hTxt1"
  ~width: 5
  ~height: 5
  ~packing:lbox#add()

 let hboxT1 = GPack.box `HORIZONTAL
  ~spacing:0
  ~border_width:0
  ~packing:hTxt1#add ()
 
 let hTxt2 = GBin.frame
  ~label:"hTxt2"
  ~width: 5
  ~height: 5
  ~packing:lbox#add()

 let hboxT2 = GPack.box `HORIZONTAL
  ~spacing:0
  ~border_width:0
  ~packing:hTxt2#add () 
   

 let grid_entry = GEdit.entry 
   ~text: "20"
    ~editable: true
   ~packing:gridTxt#add () 
    

 (*Buttons and TextEntry*)

  let min = GEdit.entry
    ~text: "10"
    ~packing:hboxT1#add () 

  let max = GEdit.entry
    ~text: "50"
    ~packing:hboxT2#add () 


let test1 = GPack.button_box `VERTICAL
  ~spacing:5
  ~packing:lbox#add ()

let test =
  let button = GButton.button
    ~label:"Validate"
    ~packing:test1#add () in
    ignore(button#connect#clicked (fun () -> inter:=
  int_of_string(grid_entry#text))); 
    ignore(button#connect#clicked (fun () -> minr:=
  int_of_string(min#text))); 
   ignore(button#connect#clicked (fun () -> maxr:=
  int_of_string(max#text)));
   ignore(button#connect#clicked (fun() -> print_int(!maxr) ));
button

 let t1 =
   let r = 0 and g = 200 and b = 0 in 
   let text = Printf.sprintf "#%02x%02x%02x" r g b in
   let entry = GEdit.entry 
     ~text: "Couleur Min"
   ~width:80 
   ~packing:hboxT1#add () in
   entry#misc#modify_base [`NORMAL,`NAME text]
 let t2 =
   let r = 255 and g = 0 and b = 0 in 
   let text = Printf.sprintf "#%02x%02x%02x" r g b in
   let entry = GEdit.entry 
     ~text: "Couleur Max"
   ~width:80 
   ~packing:hboxT2#add () in
   entry#misc#modify_base [`NORMAL,`NAME text]
    
let main () = 


ignore(window#connect#destroy ~callback:GMain.quit);
window#show ();
 GMain.main ()
 

(*Main*)
let _ = 
  main ()

